import { Component } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

/**
 * app评论页组件
 */
@Component({
  selector: 'app-mobile-comment',
  templateUrl: './mobile-comment.component.html',
  styleUrls: ['./mobile-comment.component.scss']
})
export class MobileCommentComponent {

  /**
   * @desc 获取传入的参数id值
   */
  commentVideoId:string="";
  /**
   * @desc 获取对应id的数据对象
   */
  video:any={}
    /**
   * @desc 获取VideoComment表中数据
   */
  commentList:any[]=[]


  constructor(private router:Router,private route:ActivatedRoute){
    this.route.queryParamMap.subscribe(data=>{
      this.commentVideoId=(data as any).params.id
      console.log(this.commentVideoId);
      this.getVideoCommentById()
      this.getVideoById()

    })

  }

/**
 * 
 * @param video 传入一条数据对象
 * 跳转到'/bvideo/mobile-videopage'页面
 * 传入对应对象的objecId字段数据
 */
goVideoDetail(video:any){
  console.log(video)
  this.router.navigate(['/bvideo/mobile-videopage'],{
    queryParams:{
      id: video.objectId
    }
  })
}


  /**
   * 
   * 获取VideoWork表中对应id的数据
   * @returns 
   * 将获取的数据赋值给this.video
   */
  async getVideoById(){
    if(!this.commentVideoId ){
      return
    }
    let url = `http://metapunk.cn:9999/parse/classes/VideoWork/${this.commentVideoId}?`
    url += '&include=user'
    let response = await fetch(url, {
        "credentials": "omit",
        "headers": {
            "X-Parse-Application-Id": "dev",
        },
        "method": "GET",
        "mode": "cors"
    })
    let data = await response.json()
    this.video = data;
    console.log(this.video)
  }

  /**
   * 
   * 获取VideoComment表中对应字段存在的数据
   * @returns 
   * 将获取的数据列表赋值给this.commentList 
   */
  async getVideoCommentById(){
    if(!this.commentVideoId){
      return
    }
    let url = `http://metapunk.cn:9999/parse/classes/VideoComment?where={"appVideo":"${this.commentVideoId}"}`
    url += '&include=user,appVideo'+'&order=-countLike'
    let response = await fetch(url, {
        "credentials": "omit",
        "headers": {
            "X-Parse-Application-Id": "dev",
        },
        "method": "GET",
        "mode": "cors"
    })
    let data = await response.json()
    this.commentList = data.results;
    console.log(this.commentList)
  }

}



